#!/usr/bin/python
# vim:fileencoding=utf8

import optparse
import os
import sys

from gnTai.knowledge.language import EnglishKnowledge
from gnTai.thoughts.english_text_2_thought import EnglishText2Thought
from gnTai.aiexception import AIException
from gnTai.english.understander import Understander

DICTIONARY_PATH = os.path.join('..', 'data', 'words.xml')

def parse_options(argv):
    parser = optparse.OptionParser()
    parser.add_option('-t', '--test', action='store_true', dest='test',
            help='run program tests', default=False)
    parser.add_option('--dictionary', action='store', dest='dictionary_path',
            help='specify path to the dictionary', default=DICTIONARY_PATH);
    return parser.parse_args(argv[1:])

def loop(options):
    understander = Understander(options.dictionary_path)
    try:
        while True:
            text = input("Write a simple sentence (write ':q' to quit): ")
            if text == ':q':
                break
            try:
                thought = understander.understand(text)
                print "Your statement: ", thought
                print "My question   : ", thought.set_question(True)
            except AIException, e:
                print 'Error: ', str(e).decode('utf-8').encode('utf-8')
    except EOFError:
        print "\nGoodbye!"

def main(argv):
    options, argv = parse_options(argv)
    if options.test:
        import __test__
        sys.exit(__test__.result)
    else:
        loop(options)
    
if __name__ == '__main__':
    main(sys.argv)
